package com.xuerhua.yxt.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.xuerhua.yxt.reqVo.PointsReqVO;
import com.xuerhua.yxt.service.IClassPointService;
import com.xuerhua.yxt.service.IStudentPointService;
import com.xuerhua.yxt.service.ITeacherPointService;
import com.xuerhua.yxt.service.ITermInfoService;
import com.xuerhua.yxt.view.TermView;

@Component
public class PointsJob {
	
	private Logger logger = LoggerFactory.getLogger(PointsJob.class);
	
	@Autowired
	private IClassPointService classPointService;
	@Autowired
	private ITeacherPointService teacherPointService;
	@Autowired
	private IStudentPointService studentPointService;
	@Autowired
	private ITermInfoService termInfoService;

	@Scheduled(cron = "0 30 5 * * ?")
	public void initMonthJob() {
		try {
			TermView view = termInfoService.selectCurrentTerm();
			PointsReqVO vo = new PointsReqVO();
			vo.setTermId(view.getId());
			vo.setFinishYear(view.getFinishYear());
			classPointService.addClassMonthPoints(vo);
			teacherPointService.addTeacherMonthPoints(vo);
			studentPointService.addStudentMonthPoints(vo);
		} catch (Exception e) {
			logger.error("--initMonthJob error={}", e);
		}
	}
	
	@Scheduled(cron = "0 40 5 * * ?")
	public void initTermJob() {
		try {
			TermView view = termInfoService.selectCurrentTerm();
			PointsReqVO vo = new PointsReqVO();
			vo.setTermId(view.getId());
			vo.setFinishYear(view.getFinishYear());
			classPointService.addClassTermPoints(vo);
			teacherPointService.addTeacherTermPoints(vo);
			studentPointService.addStudentTermPoints(vo);
		} catch (Exception e) {
			logger.error("---initTermJob error={}", e);
		}
	}
	
	@Scheduled(cron = "0 50 5 * * ?")
	public void updateTermJob() {
		try {
			TermView view = termInfoService.selectCurrentTerm();
			PointsReqVO vo = new PointsReqVO();
			vo.setTermId(view.getId());
			vo.setFinishYear(view.getFinishYear());
			classPointService.updateClassTermPoints(vo);
			teacherPointService.updateTeacherTermPoints(vo);
			studentPointService.updateStudentTermPoints(vo);
		} catch (Exception e) {
			logger.error("--updateTermJob error={}", e);
		}
	}
	
	@Scheduled(cron = "0 10 6 * * ?")
	public void updateTotalJob() {
		try {
			TermView view = termInfoService.selectCurrentTerm();
			PointsReqVO vo = new PointsReqVO();
			vo.setTermId(view.getId());
			vo.setFinishYear(view.getFinishYear());
			classPointService.updateClassTotalPoints(vo);
			teacherPointService.updateTeacherTotalPoints(vo);
			studentPointService.updateStudentTotalPoints(vo);
		} catch (Exception e) {
			logger.error("---updateTotalJob error={}", e);
		}
	}
}
